package com.study.base.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by tan on 2017/7/4.
 */
//Java ConcurrentModificationException 异常分析与解决方案   http://www.2cto.com/kf/201403/286536.html
//java foreach实现原理  http://www.cnblogs.com/vinozly/p/5465454.html
//Why am I not getting a java.util.ConcurrentModificationException in this example? https://stackoverflow.com/questions/8189466/why-am-i-not-getting-a-java-util-concurrentmodificationexception-in-this-example


public class ConcurrentModificationExceptionTest {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add(i);
		}

		Iterator<Integer> it = list.iterator();
		while (it.hasNext()) {
			int i = it.next();
			if (2 == i) {
				it.remove();
				//list.remove(i);
			}
		}

		for (int i=0; i<list.size(); i++) {
			System.out.println(list.get(i));
			if (1 == list.get(i)) {
				list.remove(i);
				i--;
				System.out.println(1);
			}
		}

//		for (int i : list) {
//			System.out.println(i);
//			if (1 == i) {
//				list.remove(i);
//			}
//		}

	}
}
